#!/bin/bash
#
# Copyright (c) 2006 Mellanox Technologies. All rights reserved.
#
# This Software is licensed under one of the following licenses:
#
# 1) under the terms of the "Common Public License 1.0" a copy of which is
#    available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/cpl.php.
#
# 2) under the terms of the "The BSD License" a copy of which is
#    available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/bsd-license.php.
#
# 3) under the terms of the "GNU General Public License (GPL) Version 2" a
#    copy of which is available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/gpl-license.php.
#
# Licensee has the right to choose one of the above licenses.
#
# Redistributions of source code must retain the above copyright
# notice and one of the license notices.
#
# Redistributions in binary form must reproduce both the above copyright
# notice, one of the license notices in the documentation
# and/or other materials provided with the distribution.
#
#  $Id: configure 9097 2006-08-24 06:39:09Z vlad $
#

usage()
{
cat << EOF
\`configure' executes configure for userspace applications

Usage:  `basename $0` [options]

    --prefix=PREFIX  for userspace components []
    --build_root=BUILD_ROOT  root directory to build RPM packages
    --with-quilt[=FILE]  path to quilt [$(/usr/bin/which quilt  2> /dev/null)]
    --without-quilt  use patch and not quilt [no]
    --with-patchdir=DIR  path to the patches directory []
    --without-patch  don't apply any patch [no]
    --with-userspace-fixes apply fixes (patches) to userspace [yes]
    --without-userspace-fixes don't apply patches to userspace

    --libdir=DIR  object code libraries []

    --mandir=DIR  man documentation [DATAROOTDIR/man]
    --sysconfdir=DIR  read-only single-machine data [PREFIX/etc]

    --with-libibverbs include libibverbs package [yes]
    --without-libibverbs include libibverbs package [no]
    
    --with-libmthca include libmthca package [yes]
    --without-libmthca include libmthca package [no]
    
    --with-libipathverbs include libipathverbs package [yes]
    --without-libipathverbs include libipathverbs package [no]
    
    --with-libehca include libehca package [yes]
    --without-libehca include libehca package [no]
    
    --with-libcxgb3 include libcxgb3 package [yes]
    --without-libcxgb3 include libcxgb3 package [no]

    --with-libibcm  include CM package [no]
    --without-libibcm  don't include CM package [yes]
    
    --with-libsdp  include SDP package [no]
    --without-libsdp  don't include SDP package [yes]
    
    --with-librdmacm  include librdmacm package [no]
    --without-librdmacm  don't include librdmacm package [yes]
    
    --with-dapl  include DAPL package [no]
    --without-dapl  don't include DAPL package [yes]
    
    --with-management-libs  include management lib packages (libibcommon, libibmad, libibumad) [no]
    --without-management-libs  don't include management lib packages (libibcommon, libibmad, libibumad) [yes]

    --with-libibcommon      include libibcommon package [no]
    --without-libibcommon   don't include libibcommon package [yes]
    --with-libibumad        include libibumad package [no]
    --without-libibumad     don't include libibumad package [yes]
    --with-libibmad         include libibmad package [no]
    --without-libibmad      don't include libibmad package [yes]

    --with-opensm  include OSM package [no]
    --without-opensm  don't include OSM package [yes]
    
    --with-openib-diags  include diagnostic utilities [no]
    --without-openib-diags  don't include diagnostic utilities [yes]
    
    --with-perftest  include perftest package [no]
    --without-perftest  don't include perftest package [yes]
    
    --with-srptools  include srptools package [no]
    --without-srptools  don't include srptools package [yes]
    
    --with-ipoibtools  include ipoibtools package [no]
    --without-ipoibtools  don't include ipoibtools package [yes]
    
    --with-qlvnictools  include qlvnictools package [no]
    --without-qlvnictools  don't include qlvnictools package [yes]

    --with-tvflash  include tvflash package [no]
    --without-tvflash  don't include tvflash package [yes]
    
    --with-mstflint  include mstflint package [no]
    --without-mstflint  don't include mstflint package [yes]
    
    --with-sdpnetstat  include sdpnetstat package [no]
    --without-sdpnetstat  don't include sdpnetstat package [yes]

    --with-all-libs  include all available packages [no]

    --help - print out options


EOF

}

# Execute command w/ echo and exit if it fail
ex()
{
        echo "$@"
        if ! "$@"; then
                printf "\nFailed executing $@\n\n"
                exit 1
        fi
}

apply_userspace_fixes()
{

    if [ ! -d ${CWD}/user_patches/fixes ]; then
        echo "${CWD}/user_patches/fixes directory does not exist"
        return 0
    fi
    for patch in ${CWD}/user_patches/fixes/*
    do
        if [ -e  ${patch} ]; then
            printf "\t${patch}\n"
            if [ "${WITH_QUILT}" == "yes" ]; then
                    ex $QUILT import ${patch}
                    ex $QUILT push patches/${patch##*/}
            else
                    if ! (patch -p1 -l < ${patch} ); then
                        echo "Failed to apply patch: ${patch}"
                        exit 1
                    fi
            fi
        fi   
    done
}

# Apply patches
apply_patch()
{
    ex mkdir -p ${CWD}/patches
    quiltrc=${CWD}/patches/quiltrc
    ex touch ${quiltrc}

cat << EOF >> ${quiltrc}
QUILT_DIFF_OPTS='-x .svn -p --ignore-matching-lines=\$Id'
QUILT_PATCH_OPTS='-l'
EOF

        QUILT="${QUILT} --quiltrc ${quiltrc}"

        if [ -n "${PATCH_DIR}" ]; then
                # Apply user's patches
                for patch in ${PATCH_DIR}/*
                do
                    if [ -e  ${patch} ]; then
                        printf "\t${patch}\n"
                        if [ "${WITH_QUILT}" == "yes" ]; then
                                ex $QUILT import ${patch}
                                ex $QUILT push patches/${patch##*/}
                        else
                                if ! (patch -p1 -l < ${patch} ); then
                                    echo "Failed to apply patch: ${patch}"
                                    exit 1
                                fi
                        fi
                    fi
                done

        else
                # Apply userspace fixes
                if [ "X${WITH_USERSPACE_FIXES}" == "Xyes" ]; then
                    apply_userspace_fixes
                fi

        fi
}

main()
{
        # Parsing parameters
        while [ ! -z "$1" ]
        do
                case $1 in
                        -prefix | --prefix | --prefi | --pref | --pre | --pr | --p | -p)
                                shift
                                prefix=$1
                                PREFIX="--prefix ${prefix}"
                        ;;
                        -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=* | -p=*)
                                prefix=`expr "x$1" : 'x[^=]*=\(.*\)'`
                                PREFIX="--prefix ${prefix}"
                        ;;
                        -libdir | --libdir)
                                shift
                                libdir=$1
                                LIBDIR="--libdir ${libdir}"
                        ;;
                        -libdir=* | --libdir=*)
                                libdir=`expr "x$1" : 'x[^=]*=\(.*\)'`
                                LIBDIR="--libdir ${libdir}"
                        ;;
                        -mandir | --mandir)
                                shift
                                mandir=$1
                                MANDIR="--mandir=${mandir}"
                        ;;
                        -mandir=* | --mandir=*)
                                mandir=`expr "x$1" : 'x[^=]*=\(.*\)'`
                                MANDIR="--mandir=${mandir}"
                        ;;
                        -sysconfdir | --sysconfdir)
                                shift
                                sysconfdir=$1
                                SYSCONFDIR="--sysconfdir=${sysconfdir}"
                        ;;
                        -sysconfdir=* | --sysconfdir=*)
                                sysconfdir=`expr "x$1" : 'x[^=]*=\(.*\)'`
                                SYSCONFDIR="--sysconfdir=${sysconfdir}"
                        ;;
                        -with-quilt | --with-quilt)
                                WITH_QUILT="yes"
                                if [ ! -z "$2" ] && [ "`echo -n $2 | cut -c 1`" != '-' ]; then
                                        shift
                                        QUILT=$1
                                        if [ ! -x ${QUILT} ]; then
                                                echo "${QUILT} does not exist"
                                                exit 1
                                        fi
                                fi
                        ;;
                        -with-quilt=* | --with-quilt=*)
                                WITH_QUILT="yes"
                                QUILT=`expr "x$1" : 'x[^=]*=\(.*\)'`
                                if [ ! -x ${QUILT} ]; then
                                        echo "${QUILT} does not exist"
                                        exit 1
                                fi
                        ;;
                        --without-quilt)
                                WITH_QUILT="no"
                        ;;
                        -with-patchdir | --with-patchdir)
                                shift
                                WITH_PATCH="yes"
                                PATCH_DIR=$1
                        ;;
                        -with-patchdir=* | --with-patchdir=*)
                                PATCH_DIR=`expr "x$1" : 'x[^=]*=\(.*\)'`
                                WITH_PATCH="yes"
                        ;;
                        --without-patch)
                                WITH_PATCH="no"
                        ;;
                        --with-userspace-fixes)
                                WITH_USERSPACE_FIXES="yes"
                                WITH_PATCH="yes"
                        ;;
                        --without-userspace-fixes)
                                WITH_USERSPACE_FIXES="no"
                        ;;
                        --with-libibverbs)
                                WITH_IBVERBS="yes"
                        ;;
                        --without-libibverbs)
                                WITH_IBVERBS="no"
                        ;;
                        --with-libmthca)
                                WITH_MTHCA="yes"
                                WITH_IBVERBS="yes"
                        ;;
                        --without-libmthca)
                                WITH_MTHCA="no"
                        ;;
                        --with-libipathverbs)
                                WITH_IPATHVERBS="yes"
                                WITH_IBVERBS="yes"
                        ;;
                        --without-libipathverbs)
                                WITH_IPATHVERBS="no"
                        ;;
                        --with-libehca)
                                WITH_EHCA="yes"
                                WITH_IBVERBS="yes"
                        ;;
                        --without-libehca)
                                WITH_EHCA="no"
                        ;;
                        --with-libcxgb3)
                                WITH_CXGB3="yes"
                                WITH_IBVERBS="yes"
                        ;;
                        --without-libcxgb3)
                                WITH_CXGB3="no"
                        ;;
                        --with-libibcm)
                                WITH_CM="yes"
                                WITH_MTHCA="yes"
                                WITH_IBVERBS="yes"
                        ;;
                        --without-libibcm)
                                WITH_CM="no"
                        ;;
                        --with-libsdp)
                                WITH_SDP="yes"
                        ;;
                        --without-libsdp)
                                WITH_SDP="no"
                        ;;
                        --with-librdmacm)
                                WITH_RDMACM="yes"
                                WITH_MTHCA="yes"
                                WITH_IBVERBS="yes"
                        ;;
                        --without-librdmacm)
                                WITH_RDMACM="no"
                        ;;
                        --with-dapl)
                                WITH_DAPL="yes"
                                WITH_RDMACM="yes"
                                WITH_MTHCA="yes"
                                WITH_IBVERBS="yes"
                        ;;
                        --without-dapl)
                                WITH_DAPL="no"
                        ;;
                        --with-management-libs)
                                WITH_MANAGEMENT_LIBS="yes"
                                WITH_IBCOMMON="yes"
                                WITH_IBUMAD="yes"
                                WITH_IBMAD="yes"
                        ;;
                        --without-management-libs)
                                WITH_MANAGEMENT_LIBS="no"
                        ;;
                        --with-libibcommon)
                                WITH_IBCOMMON="yes"
                                WITH_MANAGEMENT_LIBS="yes"
                        ;;
                        --without-libibcommon)
                                WITH_IBCOMMON="no"
                        ;;
                        --with-libibumad)
                                WITH_IBUMAD="yes"
                                WITH_MTHCA="yes"
                                WITH_IBVERBS="yes"
                                WITH_IBCOMMON="yes"
                                WITH_MANAGEMENT_LIBS="yes"
                        ;;
                        --without-libibumad)
                                WITH_IBUMAD="no"
                        ;;
                        --with-libibmad)
                                WITH_IBMAD="yes"
                                WITH_IBUMAD="yes"
                                WITH_MTHCA="yes"
                                WITH_IBVERBS="yes"
                                WITH_IBCOMMON="yes"
                                WITH_MANAGEMENT_LIBS="yes"
                        ;;
                        --without-libibmad)
                                WITH_IBMAD="no"
                        ;;
                        --with-opensm)
                                WITH_OSM="yes"
                                WITH_IBUMAD="yes"
                                WITH_IBCOMMON="yes"
                                WITH_MTHCA="yes"
                                WITH_IBVERBS="yes"
                                WITH_MANAGEMENT_LIBS="yes"
                        ;;
                        --without-opensm)
                                WITH_OSM="no"
                        ;;
                        --with-openib-diags)
                                WITH_DIAGS="yes"
                                WITH_OSM="yes"
                                WITH_IBMAD="yes"
                                WITH_IBUMAD="yes"
                                WITH_MTHCA="yes"
                                WITH_IBVERBS="yes"
                                WITH_IBCOMMON="yes"
                                WITH_MANAGEMENT_LIBS="yes"
                        ;;
                        --without-openib-diags)
                                WITH_DIAGS="no"
                        ;;
                        --with-perftest)
                                WITH_PERFTEST="yes"
                                WITH_MTHCA="yes"
                                WITH_IBVERBS="yes"
                                WITH_RDMACM="yes"
                        ;;
                        --without-perftest)
                                WITH_PERFTEST="no"
                        ;;
                        --with-srptools)
                                WITH_SRPTOOLS="yes"
                                WITH_IBVERBS="yes"
                                WITH_IBCOMMON="yes"
                                WITH_IBUMAD="yes"
                                WITH_MANAGEMENT_LIBS="yes"
                        ;;
                        --without-srptools)
                                WITH_SRPTOOLS="no"
                        ;;
                        --with-ipoibtools)
                                WITH_IPOIBTOOLS="yes"
                        ;;
                        --without-ipoibtools)
                                WITH_IPOIBTOOLS="no"
                        ;;
                        --with-qlvnictools)
                                WITH_QLVNICTOOLS="yes"
                                WITH_IBVERBS="yes"
                                WITH_IBCOMMON="yes"
                                WITH_IBUMAD="yes"
                                WITH_MANAGEMENT_LIBS="yes"
                        ;;
                        --without-qlvnictools)
                                WITH_QLVNICTOOLS="no"
                        ;;
                        --with-tvflash)
                                WITH_TVFLASH="yes"
                        ;;
                        --without-tvflash)
                                WITH_TVFLASH="no"
                        ;;
                        --with-mstflint)
                                WITH_MSTFLINT="yes"
                        ;;
                        --without-mstflint)
                                WITH_MSTFLINT="no"
                        ;;
                        --with-sdpnetstat)
                                WITH_SDPNETSTAT="yes"
                        ;;
                        --without-sdpnetstat)
                                WITH_SDPNETSTAT="no"
                        ;;
                        --with-all-libs)
                                WITH_CM="yes"
                                # WITH_DAPL="yes"
                                WITH_DIAGS="yes"
                                # WITH_EHCA="yes"
                                WITH_CXGB3="yes"
                                WITH_IBCOMMON="yes"
                                WITH_IBMAD="yes"
                                WITH_IBUMAD="yes"
                                WITH_IBVERBS="yes"
                                # WITH_IPATHVERBS="yes"
                                WITH_IPOIBTOOLS="yes"
                                WITH_MANAGEMENT_LIBS="yes"
                                WITH_MSTFLINT="yes"
                                WITH_SDPNETSTAT="yes"
                                WITH_MTHCA="yes"
                                WITH_OSM="yes"
                                WITH_PATCH="yes"
                                WITH_PERFTEST="yes"
                                WITH_RDMACM="yes"
                                WITH_SDP="yes"
                                WITH_SRPTOOLS="yes"
                                WITH_QLVNICTOOLS="yes"
                                WITH_TVFLASH="yes"
                        ;;
                        -h | --help)
                                usage
                                exit 0
                        ;;
                        *)
                                echo
                                echo "Wrong parameter $1"
                                echo
                                usage
                                exit 1
                        ;;
                esac
                shift

        done

#Set default values
prefix=${prefix:-"/usr/local"}
PREFIX=${PREFIX:-""}
LIBDIR=${LIBDIR:-""}
MANDIR=${MANDIR:-""}
SYSCONFDIR=${SYSCONFDIR:-""}


WITH_QUILT=${WITH_QUILT:-"yes"}
WITH_PATCH=${WITH_PATCH:-"yes"}
WITH_USERSPACE_FIXES=${WITH_USERSPACE_FIXES:-"yes"}

ARCH=${ARCH:-$(uname -m)}

# Additional parameters to be passed to configure command
WITH_IBVERBS=${WITH_IBVERBS:-"no"}
WITH_MTHCA=${WITH_MTHCA:-"no"}
WITH_IPATHVERBS=${WITH_IPATHVERBS:-"no"}
WITH_EHCA=${WITH_EHCA:-"no"}
WITH_CXGB3=${WITH_CXGB3:-"no"}
WITH_CM=${WITH_CM:-"no"}
WITH_SDP=${WITH_SDP:-"no"}
WITH_DAPL=${WITH_DAPL:-"no"}
WITH_MANAGEMENT_LIBS=${WITH_MANAGEMENT_LIBS:-"no"}
WITH_OSM=${WITH_OSM:-"no"}
WITH_DIAGS=${WITH_DIAGS:-"no"}
WITH_PERFTEST=${WITH_PERFTEST:-"no"}
WITH_SRPTOOLS=${WITH_SRPTOOLS:-"no"}
WITH_QLVNICTOOLS=${WITH_QLVNICTOOLS:-"no"}
WITH_IPOIBTOOLS=${WITH_IPOIBTOOLS:-"no"}
WITH_TVFLASH=${WITH_TVFLASH:-"no"}
WITH_MSTFLINT=${WITH_MSTFLINT:-"no"}
WITH_SDPNETSTAT=${WITH_SDPNETSTAT:-"no"}
WITH_RDMACM=${WITH_RDMACM:-"no"}

QUILT=${QUILT:-$(/usr/bin/which quilt  2> /dev/null)}
AR=${AR:-$(/usr/bin/which ar  2> /dev/null)}
CWD=$(pwd)
CONFIG="configure.mk.user"
CACHEFILE=${CWD}/configure.cache
PATCH_DIR=${PATCH_DIR:-""}


        # Check parameters
        if [ "$WITH_PATCH" == "yes" ] && [ "$WITH_QUILT" == "yes" ] && [[ ! -x ${QUILT} || ! -n "${QUILT}" ]]; then
                echo "Quilt ${QUILT} does not exist... Going to use patch."
                WITH_QUILT="no"
        fi

        # Create configure.mk
        /bin/rm -f ${CWD}/${CONFIG}
        cat >> ${CWD}/${CONFIG} << EOFCONFIG
prefix=${prefix}
PREFIX="${PREFIX}"
libdir=${libdir}

# Current working directory
CWD=${CWD}

# User level
WITH_IBVERBS=${WITH_IBVERBS}
WITH_MTHCA=${WITH_MTHCA}
WITH_IPATHVERBS=${WITH_IPATHVERBS}
WITH_EHCA=${WITH_EHCA}
WITH_CXGB3=${WITH_CXGB3}
WITH_CM=${WITH_CM}
WITH_SDP=${WITH_SDP}
WITH_DAPL=${WITH_DAPL}
WITH_RDMACM=${WITH_RDMACM}
WITH_MANAGEMENT_LIBS=${WITH_MANAGEMENT_LIBS}
WITH_OSM=${WITH_OSM}
WITH_DIAGS=${WITH_DIAGS}
WITH_PERFTEST=${WITH_PERFTEST}
WITH_SRPTOOLS=${WITH_SRPTOOLS}
WITH_IPOIBTOOLS=${WITH_IPOIBTOOLS}
WITH_QLVNICTOOLS=${WITH_QLVNICTOOLS}
WITH_TVFLASH=${WITH_TVFLASH}
WITH_MSTFLINT=${WITH_MSTFLINT}
WITH_SDPNETSTAT=${WITH_SDPNETSTAT}

EOFCONFIG
        echo "Created ${CONFIG}:"
        cat ${CWD}/${CONFIG}


        if [ "$WITH_PATCH" == "yes" ]; then
                apply_patch
        fi

        # Run configure for user level
        
        ullibs="" ## examples ## useraccess-lib
        
        if [ "${WITH_IBVERBS}" == "yes" ]; then
            ullibs="${ullibs} libibverbs"
        fi
        if [ "${WITH_MTHCA}" == "yes" ]; then
            ullibs="${ullibs} libmthca"
        fi
        if [ "${WITH_IPATHVERBS}" == "yes" ]; then
            ullibs="${ullibs} libipathverbs"
        fi
        if [ "${WITH_EHCA}" == "yes" ]; then
            ullibs="${ullibs} libehca"
        fi
        if [ "${WITH_CXGB3}" == "yes" ]; then
            ullibs="${ullibs} libcxgb3"
        fi
        if [ "${WITH_CM}" == "yes" ]; then
            ullibs="${ullibs} libibcm"
        fi
        if [ "${WITH_SDP}" == "yes" ]; then
            ullibs="${ullibs} libsdp"
        fi
        if [ "${WITH_DAPL}" == "yes" ]; then
            ullibs="${ullibs} dapl"
        fi
        if [ "${WITH_MSTFLINT}" == "yes" ]; then
            ullibs="${ullibs} mstflint"
        fi
        if [ "${WITH_SDPNETSTAT}" == "yes" ]; then
            ullibs="${ullibs} sdpnetstat"
        fi
        if [ "${WITH_PERFTEST}" == "yes" ]; then
            ullibs="${ullibs} perftest"
        fi
        if [ "${WITH_SRPTOOLS}" == "yes" ]; then
            ullibs="${ullibs} srptools"
        fi
        if [ "${WITH_QLVNICTOOLS}" == "yes" ]; then
            ullibs="${ullibs} qlvnictools/ibvexdmtools"
        fi
        if [ "${WITH_TVFLASH}" == "yes" ]; then
            ullibs="${ullibs} tvflash"
        fi
        if [ "${WITH_RDMACM}" == "yes" ]; then
            ullibs="${ullibs} librdmacm"
        fi
        if [ "${WITH_DAPL}" == "yes" ]; then
            ullibs="${ullibs} dapl"
        fi
        
        ex /bin/rm -f ${CACHEFILE}

        for dir in ${ullibs}
        do
                ex cd ${CWD}/src/userspace/${dir}
                
                ENV="env \
ac_cv_lib_ibverbs_ibv_get_device_list=yes \
ac_cv_header_infiniband_driver_h=yes \
ac_cv_func_ibv_read_sysfs_file=yes \
ac_cv_func_ibv_dontfork_range=yes \
ac_cv_func_ibv_dofork_range=yes \
ac_cv_func_ibv_register_driver=yes \
HAVE_IBV_DEVICE_LIBRARY_EXTENSION_TRUE=yes \
"

                [ ! -x ./configure ] && [ ! -x ./autogen.sh ] && continue
                [ ! -x ./configure ] && ./autogen.sh 
        
		${AR} qc libibverbs.a
                echo "Running: $ENV ./configure --cache-file=${CACHEFILE} --disable-libcheck ${PREFIX} ${LIBDIR} ${MANDIR} ${SYSCONFDIR} CPPFLAGS=\"-I../libibverbs/include\""
                if ! ( $ENV ./configure --cache-file=${CACHEFILE} --disable-libcheck ${PREFIX} ${LIBDIR} ${MANDIR} ${SYSCONFDIR} \
                        CPPFLAGS="-I../libibverbs/include" LDFLAGS="-L.")
                then
                        echo "Failed to execute: cd ${CWD}/src/userspace/${dir} && $ENV ./configure --cache-file=${CACHEFILE} --disable-libcheck ${PREFIX} ${LIBDIR} ${MANDIR} ${SYSCONFDIR} CPPFLAGS=\"-I../libibverbs/include\""
                        /bin/rm -f libibverbs.a
                        exit 1
                fi
                /bin/rm -f libibverbs.a
        done
        
        ex /bin/rm -f ${CACHEFILE}
        
        if [ "${WITH_MANAGEMENT_LIBS}" == "yes" ]; then                                            
                ex cd ${CWD}/src/userspace/management
                manage_dirs="libibcommon libibumad libibmad"
                
                if [ "${WITH_OSM}" == "yes" ]; then
                    manage_dirs="${manage_dirs} osm"
                fi
        
                if [ "${WITH_DIAGS}" == "yes" ]; then
                        manage_dirs="${manage_dirs} diags"
                fi
        
                for dir in ${manage_dirs}
                do
                        ( cd ${dir} &&
                                [ ! -x ./configure ] && [ ! -x ./autogen.sh ] ) && continue
                        ( cd ${dir} && 
                                [ ! -x ./configure ] && ./autogen.sh )
                        echo "Running: cd ${dir} && ./configure --cache-file=${CACHEFILE} --disable-libcheck --disable-console-socket ${PREFIX} ${LIBDIR} ${MANDIR} ${SYSCONFDIR}"
                        if ! ( cd ${dir} && ./configure --cache-file=${CACHEFILE} --disable-libcheck --disable-console-socket ${PREFIX} ${LIBDIR} ${MANDIR} ${SYSCONFDIR} )
                        then
                                echo "Failed to execute cd ${dir} && ./configure --cache-file=${CACHEFILE} --disable-libcheck --disable-console-socket ${PREFIX} ${LIBDIR} ${MANDIR} ${SYSCONFDIR}"
                                exit 1
                        fi            
                done
                ex /bin/rm -f ${CACHEFILE}
        fi                
}

main $@
